package com.cn.mmd.controller;


import com.cn.mmd.model.*;
import com.cn.mmd.service.EvaluateTeacherService;
import com.cn.mmd.service.LabelService;
import com.cn.mmd.tools.GetLimitNum;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 标签控制类
 * Created by Administrator on 2016/8/1.
 */

@Controller

@RequestMapping(value = "Label")
public class LabelController {


    @Autowired
    LabelService labelService;
    @Autowired
    EvaluateTeacherService evaluateTeacherService;
    @Autowired
    LogController logController;
    @Autowired
    TokenController tokenController;


    /**
     * 查看所有标签库
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectPackageLabel")
    public synchronized @ResponseBody Map<String, Object> selectPackageLabel(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        GetLimitNum G=new GetLimitNum();

        int startrow=Integer.parseInt(request.getParameter("startrow"));//查第几页 >=1
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));//一页多少条数据

        int num=labelService.selectPackageCount();
        //System.out.println("num的数量："+num);

        num=G.getLimitNum(num, pageSize);

        //System.out.println("num的数量+："+num);
        int page=(startrow-1)*pageSize;//从哪一条开始
        //System.out.println("page的值："+page);

        LabelPackage labelPackage=new LabelPackage(page,pageSize);

        List<LabelPackage> packageList=labelService.selectAllPackage(labelPackage);

        if (packageList.size()>0){

            map1.put("list",packageList);
            map1.put("pages",num);

            //System.out.println("成功");
            map.put("code", 200);
            map.put("message", "查询成功");
            map.put("data", map1);
        }else {
            //System.out.println("查询失败");
            map.put("code", 400);
            map.put("message", "失败");
//            map.put("data", "");
        }


        return map;
    }




    /**
     * 查看一级指标（通过标签库名查出下面的所有一级指标名）
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectIndexName")
    public synchronized
    @ResponseBody
    Map<String, Object> selectIndexName(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String packageName = request.getParameter("packageName");//标签库名

        LabelPackage labelPackage=labelService.selectLabelPackage(packageName);
        if (labelPackage==null){

            map.put("code", 201);
            map.put("message", "没有这套标签库");
//            map.put("data", "");

        }else {
            List<LabelIndex> labelIndexes=labelService.selectByPackageName(packageName);

            if (labelIndexes.size()>0){

                map.put("code", 200);
                map.put("message", "查询成功");
                map.put("data", labelIndexes);
            }else {
                map.put("code", 202);
                map.put("message", "这套标签无一级指标，请添加");
//                map.put("data", "");
            }
        }

        return map;
    }



    /**
     * 查看二级指标（通过一级指标名查出下面的所有二级指标名）
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectTwoIndexName")
    public synchronized
    @ResponseBody
    Map<String, Object> selectTwoIndexName(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String indexName = request.getParameter("indexName");//一级指标名
        String packageName=request.getParameter("packageName");//标签库名

        LabelTwoIndex labelTwoIndex=new LabelTwoIndex(indexName,packageName);

        List<LabelTwoIndex> labelTwoIndexes = labelService.selectBylabelName(labelTwoIndex);

        if (labelTwoIndexes.size() > 0) {


            map.put("code", 200);
            map.put("message", "查询成功");
            map.put("data", labelTwoIndexes);

        } else {
            map.put("code", 201);
            map.put("message", "当前一级指标下无二级指标，请添加");
//            map.put("data", labelTwoIndexes);
        }

        return map;
    }


    /**
     * 查看三级指标（通过二级指标名查出下面的所有三级指标名）
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectThreeIndexName")
    public synchronized
    @ResponseBody
    Map<String, Object> selectThreeIndexName(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String twoIndexName = request.getParameter("twoIndexName");//二级指标名
        String packageName = request.getParameter("packageName");//标签库名

        LabelThreeIndex labelThreeIndex = new LabelThreeIndex();
        labelThreeIndex.setTwoindexname(twoIndexName);
        labelThreeIndex.setPackagelabel(packageName);


        List<LabelThreeIndex> labelThreeIndexes = labelService.selectByTwoLabelName(labelThreeIndex);

        if (labelThreeIndexes.size() > 0) {
            map.put("code", 200);
            map.put("message", "查询成功");
            map.put("data", labelThreeIndexes);
        } else {
            map.put("code", 201);
            map.put("message", "当前二级指标下无三级指标，请添加");
//            map.put("data", labelThreeIndexes);
        }
        return map;
    }


    /**
     * 查看所有三级指标（通过标签库查出下面的所有三级指标名）
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectAllThreeIndexName")
    public synchronized
    @ResponseBody
    Map<String, Object> selectAllThreeIndexName(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        //String twoIndexName = request.getParameter("twoIndexName");//二级指标名
        String packageName = request.getParameter("packageName");//标签库名

        LabelThreeIndex labelThreeIndex = new LabelThreeIndex();
        labelThreeIndex.setPackagelabel(packageName);

        List<LabelThreeIndex> labelThreeIndexes = labelService.selectByTwoLabelName(labelThreeIndex);

        if (labelThreeIndexes.size() > 0) {
            map.put("code", 200);
            map.put("message", "查询成功");
            map.put("data", labelThreeIndexes);
        } else {
            map.put("code", 201);
            map.put("message", "当前二级指标下无三级指标，请添加");
//            map.put("data", labelThreeIndexes);
        }
        return map;
    }

    /**
     * 查看标签（通过三级指标名查出下面的所有标签）
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectLabelName")
    public synchronized @ResponseBody
    Map<String, Object> selectLabelName(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String threeIndexName = request.getParameter("threeIndexName");//三级指标名
        String packageName = request.getParameter("packageName");//标签库名

        Label label = new Label();
        label.setThreeindexname(threeIndexName);
        label.setPackagelabel(packageName);

        List<Label> labels = labelService.selectByThreeLabelName(label);

        if (labels.size() > 0) {
            map.put("code", 200);
            map.put("message", "查询成功");
            map.put("data", labels);
        } else {
            map.put("code", 201);
            map.put("message", "当前三级指标下无标签，请添加");
//            map.put("data", labels);
        }
        return map;
    }

    /**
     * 查看所有标签（通过标签库查出下面的所有标签）
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectAllLabelName")
    public synchronized
    @ResponseBody
    Map<String, Object> selectAllLabelName(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

//        String threeIndexName = request.getParameter("threeIndexName");//三级指标名
        String packageName = request.getParameter("packageName");//标签库名
        String operatorId=request.getParameter("operatorId");//操作人id
        String operatorType=request.getParameter("operatorType");//操作人类型
        String token=request.getParameter("token");

        if (operatorType.equals("教师")){

            TeacherData teacherData;
            teacherData = (TeacherData) tokenController.token(operatorId, token);
            if (teacherData==null){
                map.put("code", 202);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }

        }else if (operatorType.equals("学生")){
            StudentApp studentApp;
            studentApp = (StudentApp) tokenController.token1(operatorId, token);
            if (studentApp==null){
                map.put("code", 202);
                map.put("message", "您暂无权限做此操作");
//                map.put("data", "");
                return map;
            }
        }

        LabelThreeIndex labelThreeIndex = new LabelThreeIndex();
        labelThreeIndex.setPackagelabel(packageName);

        List<LabelThreeIndex> labelThreeIndexes = labelService.selectByTwoLabelName(labelThreeIndex);


        if (labelThreeIndexes.size() > 0) {
            List<Map<String, Object>> mapList=new ArrayList<Map<String, Object>>();

            for (int i=0;i<labelThreeIndexes.size();i++){
                Map<String, Object> map1 = new HashMap<String, Object>();
                List<Map<String, Object>> mapList1=new ArrayList<Map<String, Object>>();

                Label label = new Label();
                label.setThreeindexname(labelThreeIndexes.get(i).getThreeindexname());
                label.setPackagelabel(packageName);

                List<Label> labels = labelService.selectByThreeLabelName(label);

                if (labels.size()>0){
                    for (int n=0;n<labels.size();n++){
                        Map<String, Object> map2 = new HashMap<String, Object>();
                        map2.put("labelName",labels.get(n).getLabelname());
                        mapList1.add(map2);
                    }
                }
                map1.put("packageName",labelThreeIndexes.get(i).getPackagelabel());
                map1.put("indexName",labelThreeIndexes.get(i).getIndexname());
                map1.put("threeindexname",labelThreeIndexes.get(i).getThreeindexname());
                map1.put("threeIndexQuestionTea",labelThreeIndexes.get(i).getThreeindexquestiontea());
                map1.put("label",mapList1);
            //    map1.put("labelGrade",labels.get(i).getLabelgrade());
                mapList.add(map1);
            }

            map.put("code", 200);
            map.put("message", "查询成功");
            map.put("data", mapList);
        } else {
            map.put("code", 201);
            map.put("message", "当前标签库无三级指标，请添加");
//            map.put("data", "");
        }
        return map;
    }

    /**
     * 修改标签
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateLabel")
    public synchronized
    @ResponseBody
    Map<String, Object> updateLabel(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String manage=request.getParameter("teacherId");//登录后台管理系统的老师的id，要添加日志

        String labelName = request.getParameter("labelName");//要修改的标签名
        String packageName = request.getParameter("packageName");//当前标签所在的标签库名
        String newlabelName = request.getParameter("newlabelName");//修改后的标签名
        String labelG=request.getParameter("labelGrade");//修改后的标签分
        Label l1=new Label();
        l1.setPackagelabel(packageName);
        l1.setLabelname(labelName);

        Label label = evaluateTeacherService.selectAllLabel(l1);

        if(label!=null){



            Float labelGrade=0f;
            if (labelG!=null&&!labelG.equals("")){
                labelGrade = Float.valueOf(labelG);//修改后的标签分数
            }else {
                //System.out.println("进入为空选项");
                labelGrade=label.getLabelgrade();
            }

            Label l=new Label();
            l.setPackagelabel(packageName);
            l.setLabelname(newlabelName);

            Label label0 = evaluateTeacherService.selectAllLabel(l);

            if (label0!=null) {
                //System.out.println(label0.getIndexname()+"==="+label0.getTwoindexname()+"==="+label0.getLabelname());
                //如果当前标签库下已有与修改后的标签相同的标签名，则修改不成功，重新修改(一个标签库下不能有多个相同的标签名)
                map.put("code", 201);
                map.put("message", "标签名“" + newlabelName + "”已存在");
//                map.put("data", "");
                return map;
            }


            // Label label = evaluateTeacherService.selectAllLabel(l1);
            Label label1 = new Label();
            label1.setLabelid(label.getLabelid());
            label1.setLabelname(newlabelName);
            label1.setLabelgrade(labelGrade);

            int b = labelService.updateLabel(label1);

            if (b > 0) {
                Label l2=new Label();
                l2.setPackagelabel(packageName);
                l2.setLabelname(newlabelName);

                //添加日志
                //操作内容
                String logContent="将标签“"+labelName+"”修改为“"+newlabelName+"”成功";

                logController.logManage(logContent, manage);

                Label label2 = evaluateTeacherService.selectAllLabel(l2);
                map.put("code", 200);
                map.put("message", "修改成功");
                map.put("data", label2);
            } else {
                map.put("code", 202);
                map.put("message", "修改失败");
//                map.put("data", "");
            }


        }else {
            //未找到要修改的项
            map.put("code", 203);
            map.put("message", "未找到要修改的项");
//            map.put("data", "");
        }

        return map;
    }

    /**
     * 添加标签
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "insertLabel")
    public synchronized
    @ResponseBody
    Map<String, Object> insertLabel(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String allInformation=request.getParameter("allInformation");//前台传入的所有信息
        //格式：{"teacherId":"教师id","packageName":"标签库名","threeIndexName":"所属三级指标名","labels":[{"labelName":"标签名1","labelGrade":"3"},
        //{"labelName":"标签名2","labelGrade":"4"}]}

        JSONObject json=JSONObject.fromObject(allInformation);//解析
       // String teacherId=json.getString("teacherId");//教师id  要添加日志
        String manage=json.getString("teacherId");//登录后台管理系统的老师的id，要添加日志
        String packageName=json.getString("packageName");//标签库名
        String threeIndexName=json.getString("threeIndexName");//所属三级指标名
        String labels=json.getString("labels");//添加的label的数组


        JSONArray labelArray=JSONArray.fromObject(labels);//解析数组

//        String labelName=request.getParameter("labelName");
//        Float labelGrade=Float.parseFloat(request.getParameter("labelGrade"));

        LabelThreeIndex labelThreeIndex=new LabelThreeIndex();
        labelThreeIndex.setPackagelabel(packageName);
        labelThreeIndex.setThreeindexname(threeIndexName);

        LabelThreeIndex labelThreeIndex1=evaluateTeacherService.selectByThreeIndexName(labelThreeIndex);

        if (labelThreeIndex1==null){
            map.put("code", 202);
            map.put("message", "三级指标名查询失败");
//            map.put("data", "");
            return map;
        }
        int a=0;
        List li=new ArrayList();
        for (int i=0;i<labelArray.size();i++){
            JSONObject labelObject=labelArray.getJSONObject(i);
            String labelName=labelObject.getString("labelName");//标签名
            Float labelGrade=Float.parseFloat(labelObject.getString("labelGrade"));//标签分数

            Label l1=new Label();
            l1.setLabelname(labelName);
            l1.setPackagelabel(packageName);

            Label label2=evaluateTeacherService.selectAllLabel(l1);//查看该标签是否已经存在于该标签库下
            if (label2!=null){
                map.put("code", 201);
                map.put("message", "标签名“" + labelName + "”已存在");
//                map.put("data", "");
                return map;
            }

            Label label=new Label();

            label.setPackagelabel(packageName);
            label.setIndexname(labelThreeIndex1.getIndexname());
            label.setTwoindexname(labelThreeIndex1.getTwoindexname());
            label.setThreeindexname(labelThreeIndex1.getThreeindexname());
            label.setLabelname(labelName);
            label.setLabelgrade(labelGrade);

            a=labelService.insertLabel(label);

            if (a>0) {
                //添加日志！！
                //操作内容
                String logContent = "添加标签“"+labelName+"”成功";
                //System.out.println(logContent);
               // LogController lc = new LogController();
                logController.logManage(logContent, manage);
                li.add(a);
            }else {
                map.put("code", 400);
                map.put("message", "添加失败");
//                map.put("data", "");
            }
        }

       // System.out.println("li的长度："+li.size());
        if (li.size()==labelArray.size()){

            map.put("code", 200);
            map.put("message", "添加成功");
            map.put("data", labelArray);
        }else {
            map.put("code", 400);
            map.put("message", "添加失败");
//            map.put("data", "");
        }

        return map;
    }



    /**
     * 删除标签
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "deleteLabel")
    public synchronized
    @ResponseBody
    Map<String, Object> deleteLabel(HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String manage=request.getParameter("teacherId");//登录后台管理系统的老师的id，要添加日志

        String labelName = request.getParameter("labelName");//前端传入要删除的标签名
        String packageName = request.getParameter("packageName");//前端传入当前标签所在的标签库名

        Label label=new Label();
        label.setPackagelabel(packageName);
        label.setLabelname(labelName);

        Label label2=evaluateTeacherService.selectAllLabel(label);

        if (label2!=null){
            int a=labelService.deleteLabel(label2.getLabelid());
            if (a>0){

                //添加日志！！
                //操作内容
                String logContent="删除标签“"+labelName+"”成功";
                //System.out.println(logContent);
                logController.logManage(logContent, manage);

                map.put("code", 200);
                map.put("message", "删除成功");
//                map.put("data", 1);
            }else {
                map.put("code", 400);
                map.put("message", "删除失败");
//                map.put("data", "");
            }
        }else {
            map.put("code", 201);
            map.put("message", "未找到要删除的标签");
//            map.put("data", "");
        }

        return map;
    }


    /**
      * 添加标签库
      * @param request
      * @return
      * @throws Exception
      */
    @RequestMapping(value = "insertPackageLabel")
    public synchronized @ResponseBody Map<String,Object> insertPackageLabel(HttpServletRequest request)throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String teacherId=request.getParameter("teacherId");//教师id
        String packageName=request.getParameter("packageName");//标签库名
        String packageExplain=request.getParameter("packageExplain");//标签库说明

        LabelPackage lp=new LabelPackage(packageName,packageExplain);

        LabelPackage labelPackage=labelService.selectLabelPackage(packageName);
        if (labelPackage!=null){
            map.put("code", 201);
            map.put("message", "该标签库已存在");
//            map.put("data", labelPackage);
        }else {

            int a=labelService.insertLabelPackage(lp);

            if (a>0){

                //添加日志
                //操作内容
                String logContent="标签库“"+packageName+"”添加成功";
                //System.out.println(logContent);
                logController.logManage(logContent, teacherId);

                map.put("code", 200);
                map.put("message", "标签库添加成功");
                map.put("data", lp);

            }else {
                map.put("code", 400);
                map.put("message", "标签库添加失败");
//                map.put("data", "");
            }
        }
        return map;
    }



    /**
     * 添加一级指标
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "insertIndexLabel")
    public synchronized @ResponseBody Map<String,Object> insertIndexLabel(HttpServletRequest request)throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String allInformation=request.getParameter("allInformation").toString();//传入的所有信息
        //allInformation：{"teacherId":"5177","packageName":"活动标签","insert":[{"indexName":"一级指标1","proportion":0.3},
        // {"indexName":"一级指标2","proportion":0.25}],"update":[{"indexName":"一级指标3","proportion":0.2},
        // {"indexName":"一级指标4","proportion":0.25}]}

        //index为json类型，insert内包括要添加的指标及其权重，update内是之前标签库里的要修改权重的指标和修改后的权重
        //所有权重相加必须=1   要添加的指标名不能与原有指标名相同
        JSONObject json=JSONObject.fromObject(allInformation);
        String teacherId=json.getString("teacherId");//教师id  要添加日志
        String packageName=json.getString("packageName");//所属标签库

        String insert=json.getString("insert");//添加的指标信息 jsonArray
        String update=json.getString("update");//修改的指标信息 jsonArray

        JSONArray insertArray=JSONArray.fromObject(insert);
        JSONArray updateArray=JSONArray.fromObject(update);

       // System.out.println(updateArray.size()+"-----"+updateArray1.size());

        List<LabelIndex> labelIndexes=labelService.selectByPackageName(packageName);

        //System.out.println(labelIndexes.size());
        if (labelIndexes.size()>0){

            //此标签库原本就有标签！！
            //System.out.println(labelIndexes.size()+"====="+updateArray.size());
            if (updateArray.size()!=labelIndexes.size()){//长度不对
                map.put("code", 201);
                map.put("message", "修改的指标不匹配");
//                map.put("data", "");
                return map;
            }

            List<LabelIndex> labelIndexes1=new ArrayList<LabelIndex>(labelIndexes);
            List list=new ArrayList();//存所有权重的list
            //System.out.println("复制后的长得："+labelIndexes1.size());

            List indexn=new ArrayList();
            List indexn1=new ArrayList();

            for (int i=0;i<updateArray.size();i++){//比较是否相等
                JSONObject updateObject=updateArray.getJSONObject(i);
                indexn.add(updateObject.get("indexName"));
                list.add(updateObject.get("proportion"));
            }

            for (int j=0;j<labelIndexes1.size();j++){
                indexn1.add(labelIndexes1.get(j).getIndexname());
            }
           // System.out.println(indexn.size()+"   "+indexn1.size());
            Collections.sort(indexn);
            Collections.sort(indexn1);
            for (int i=0;i<indexn.size();i++){
                if (!indexn.get(i).equals(indexn1.get(i))){
                    map.put("code", 202);
                    map.put("message", "修改指标错误");
//                    map.put("data", "");
                    return map;
                }
            }

            for (int i=0;i<insertArray.size();i++) {
                JSONObject insertObject = insertArray.getJSONObject(i);
                for (int j=0;j<insertArray.size()&&j!=i;j++) {
                    JSONObject insertObject1 = insertArray.getJSONObject(j);
                    String indexName = insertObject.getString("indexName");//要添加的一级指标名
                    String indexName1 = insertObject1.getString("indexName");//要添加的一级指标名
                    if (indexName.equals(indexName1)){
                        map.put("code", 203);
                        map.put("message", "不能添加多个同名的指标");
//                        map.put("data", indexName);
                        return map;
                    }
                }
            }


            for (int i=0;i<insertArray.size();i++) {
                JSONObject insertObject = insertArray.getJSONObject(i);
                for (int j=0;j<labelIndexes.size();j++){
                    String indexName = insertObject.getString("indexName");//要添加的一级指标名
                    if (indexName.equals(labelIndexes.get(j).getIndexname())){
                        map.put("code", 204);
                        map.put("message", "指标"+indexName+"已存在");
//                        map.put("data", labelIndexes.get(j));
                        return map;
                    }
                }
                list.add(insertObject.get("proportion"));//添加加入的指标权重到list
            }
            //判断传入的权重相加为不为1
            Float sum=0f;
            for (Object o:list){
                sum+=Float.parseFloat(o.toString());
            }
            if (sum!=1){
                map.put("code", 205);
                map.put("message", "指标权重不为1，请重置");
//                map.put("data", "");
                return map;
            }

            LabelIndex labelIndex=null;
            List la=new ArrayList();
            List lb=new ArrayList();
            int a=0,b=0;
            for (int i=0;i<insertArray.size();i++) {//添加指标
                JSONObject insertObject = insertArray.getJSONObject(i);
                Float proportion=Float.parseFloat(insertObject.get("proportion").toString());
                labelIndex=new LabelIndex(insertObject.getString("indexName"),proportion,packageName);

                a=labelService.insertOneIndex(labelIndex);
                if (a>0){
                    //添加日志！！
                    //操作内容
                    String logContent="添加一级指标“"+insertObject.getString("indexName")+"”成功";
                    //System.out.println(logContent);
                    logController.logManage(logContent, teacherId);

                    la.add(a);
                }

            }
            for (int i=0;i<updateArray.size();i++) {

                JSONObject updateObject=updateArray.getJSONObject(i);
                Float proportion=Float.parseFloat(updateObject.get("proportion").toString());
                labelIndex=new LabelIndex(updateObject.getString("indexName"),proportion,packageName);

                b=labelService.updateByIndexName(labelIndex);
                if (b>0){

                    lb.add(b);
                }
            }
            List<LabelIndex> labelIndexes2=labelService.selectByPackageName(packageName);
            if (la.size()==insertArray.size()&&lb.size()==updateArray.size()) {

                map.put("code", 200);
                map.put("message", "添加一级指标成功");
                map.put("data", labelIndexes2);

            }else {
                map.put("code", 400);
                map.put("message", "添加一级指标失败");
//                map.put("data", labelIndexes2);
            }


        }else {
            //此标签库原本没有标签！！

            List list = new ArrayList();
            Float sum = 0f;
            for (int i = 0; i < insertArray.size(); i++) {
                JSONObject jsonObject = insertArray.getJSONObject(i);

                // String indexName=jsonObject.getString("indexName");//一级指标名
                Float proportion = Float.parseFloat(jsonObject.getString("proportion"));//指标权重

                sum += proportion;

            }
            //System.out.println("和：" + sum);
            if (sum != 1) {
                map.put("code", 205);
                map.put("message", "权重设置不为1，请重置");
//                map.put("data", "");
            } else {

                LabelIndex labelIndex = null;
                for (int i = 0; i < insertArray.size(); i++) {
                    JSONObject jsonObject = insertArray.getJSONObject(i);

                    String indexName = jsonObject.getString("indexName");//一级指标名
                    Float proportion = Float.parseFloat(jsonObject.getString("proportion"));//指标权重

                    labelIndex = new LabelIndex(indexName, proportion, packageName);

                    int a = labelService.insertOneIndex(labelIndex);

                    if (a>0){
                        //添加日志！！
                        //操作内容
                        String logContent="添加一级指标“"+indexName+"”成功";
                        //System.out.println(logContent);
                        logController.logManage(logContent, teacherId);

                        list.add(a);
                    }
                }

                List<LabelIndex> labelIndexes2=labelService.selectByPackageName(packageName);
                if (insertArray.size() == list.size()) {
                    //成功
                    map.put("code", 200);
                    map.put("message", "添加一级指标成功");
                    map.put("data", labelIndexes2);
                } else {
                    //失败
                    map.put("code", 400);
                    map.put("message", "添加一级指标失败");
//                    map.put("data", "");
                }
            }
        }
    return map;
    }


    /**
     * 修改一级指标
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateIndexLabel")
    public synchronized @ResponseBody Map<String,Object> updateIndexLabel(HttpServletRequest request)throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String allInformation=request.getParameter("allInformation").toString();//传入的所有信息
        //allInformation：
        // {"teacherId":"5177","packageName":"活动标签","update":[{"indexName":"一级指标1","newIndexName":"一级指标1.1","proportion":0.15},{"indexName":"一级指标2","newIndexName":"一级指标1.1","proportion":0.2},
        //{"indexName":"一级指标3","newIndexName":"一级指标1.1","proportion":0.25},{"indexName":"一级指标4","newIndexName":"一级指标1.1","proportion":0.2},{"indexName":"一级指标5","newIndexName":"一级指标1.1","proportion":0.2}]}

        //update内有三个参数，indexName为原来的指标名，newIndexName为修改后的指标名，如果不修改，则与indexName相同，proportion为修改后的权重

        JSONObject json=JSONObject.fromObject(allInformation);
        String teacherId=json.getString("teacherId");//教师id  要添加日志
        String packageName=json.getString("packageName");//所属标签库

        String update=json.getString("update");//修改的指标信息 jsonArray

        JSONArray updateArray=JSONArray.fromObject(update);

        List<LabelIndex> labelIndexes=labelService.selectByPackageName(packageName);
        if (labelIndexes.size()>0){
            List indexn=new ArrayList();
            List indexn1=new ArrayList();
            List list=new ArrayList();
            for (int i=0;i<labelIndexes.size();i++){
                indexn.add(labelIndexes.get(i).getIndexname());
            }
            JSONObject updateObject=null;
            JSONObject updateObject1=null;
            for (int i=0;i<updateArray.size();i++){
                updateObject=updateArray.getJSONObject(i);
                if (labelIndexes.size()!=updateArray.size()){
                    map.put("code", 201);
                    map.put("message", "指标不匹配");
//                    map.put("data", "");
                    return map;
                }
                for (int j=0;j<updateArray.size()&&j!=i;j++){
                    updateObject1=updateArray.getJSONObject(j);
                    if (updateObject1.get("newIndexName").equals(updateObject.get("newIndexName"))){
                        map.put("code", 202);
                        map.put("message", "指标名不能相同");
//                        map.put("data", "");
                        return map;
                    }
                }

                list.add(updateObject.get("proportion"));//权重
                indexn1.add(updateObject.get("indexName"));
            }
            Collections.sort(indexn);
            Collections.sort(indexn1);
            for (int i=0;i<indexn.size();i++){
                if (!indexn.get(i).equals(indexn1.get(i))){
                    map.put("code", 201);
                    map.put("message", "指标不匹配");
//                    map.put("data", indexn1.get(i));
                    return map;
                }
            }
            Float sum=0f;
            for (Object o:list){
                sum+=Float.parseFloat(o.toString());
            }
            if (sum!=1){
                map.put("code", 203);
                map.put("message", "指标权重不为1，请重置");
//                map.put("data", "");
                return map;
            }
            //修改指标
            LabelIndex li=null,li1=null;
            List list1=new ArrayList();
            List updateList=new ArrayList();
            LabelTwoIndex labelTwoIndex=null,labelTwoIndex1=null;
            LabelThreeIndex labelThreeIndex=null,labelThreeIndex1=null;
            Label label=null,label1=null;
            for (int i=0;i<updateArray.size();i++) {
                updateObject = updateArray.getJSONObject(i);

                updateList.add(updateObject.get("indexName"));

                li = new LabelIndex(updateObject.getString("indexName"), packageName);

                LabelIndex labelIndex = labelService.selectIndexId(li);
                if (labelIndex != null) {

                    Float proportion = Float.parseFloat(updateObject.getString("proportion"));
                    li1 = new LabelIndex(labelIndex.getIndexid(), updateObject.getString("newIndexName"), proportion);

                    int a = labelService.updateByindexId(li1);

                    if (a > 0) {
                        //添加日志
                        String logContent="修改一级指标“"+updateObject.getString("indexName")+"”为“"+updateObject.getString("newIndexName")+"”成功";
                        //System.out.println(logContent);
                        logController.logManage(logContent, teacherId);

                        list1.add(a);
                    }

                } else {
                    map.put("code", 204);
                    map.put("message", "指标查询错误");
//                    map.put("data", "");
                    return map;
                }
                  //修改二级指标表里的一级指标名

                labelTwoIndex = new LabelTwoIndex(updateObject.getString("indexName"), packageName);

                List<LabelTwoIndex> labelTwoIndexList = labelService.selectBylabelName(labelTwoIndex);

                List updateTwo = new ArrayList();
                if (labelTwoIndexList.size() > 0) {

                    for (int j = 0; j < labelTwoIndexList.size(); j++) {

                        labelTwoIndex1 = new LabelTwoIndex();
                        labelTwoIndex1.setTwoindexid(labelTwoIndexList.get(j).getTwoindexid());
                        labelTwoIndex1.setIndexname(updateObject.getString("newIndexName"));//新一级指标名

                        int b = labelService.updateByTwoIndexId(labelTwoIndex1);

                        if (b > 0) {
                            updateTwo.add(b);
                        } else {
                            map.put("code", 205);
                            map.put("message", "修改二级指标表失败");
//                            map.put("data", "");
                            return map;
                        }
                    }
                    if (labelTwoIndexList.size() == updateTwo.size()) {
                        //System.out.println("修改二级指标表成功");
                    } else {
                        map.put("code", 205);
                        map.put("message", "修改二级指标表失败");
//                        map.put("data", "");
                        return map;
                    }
                } else {
                    //System.out.println("查询无二级指标");
//                    map.put("code", 205);
//                    map.put("message", );
//                    map.put("data", updateObject.getString("indexName"));
                   // return map;
                }

                    //修改三级指标表里的一级指标名

                labelThreeIndex = new LabelThreeIndex();
                labelThreeIndex.setIndexname(updateObject.getString("indexName"));
                labelThreeIndex.setPackagelabel(packageName);

                List<LabelThreeIndex> labelThreeIndexes = labelService.selectByTwoLabelName(labelThreeIndex);

                List updateThree = new ArrayList();
                if (labelThreeIndexes.size() > 0) {

                    for (int j = 0; j < labelThreeIndexes.size(); j++) {

                        labelThreeIndex1 = new LabelThreeIndex();
                        labelThreeIndex1.setThreeindexid(labelThreeIndexes.get(j).getThreeindexid());
                        labelThreeIndex1.setIndexname(updateObject.getString("newIndexName"));//新一级指标名

                        int b = labelService.updateByThreeIndexId(labelThreeIndex1);

                        if (b > 0) {
                            updateThree.add(b);
                        } else {
                            map.put("code", 206);
                            map.put("message", "修改三级指标表失败");
//                            map.put("data", "");
                            return map;
                        }
                    }
                    if (labelThreeIndexes.size() == updateThree.size()) {
                        //System.out.println("修改三级指标表成功");
                    } else {
                        map.put("code", 206);
                        map.put("message", "修改三级指标表失败");
//                        map.put("data", "");
                        return map;
                    }
                }else {
                    //System.out.println("查询无三级指标");
//                    map.put("code", 207);
//                    map.put("message", "查询无三级指标");
//                    map.put("data", updateObject.getString("indexName"));
                   // return map;
                }


                //修改标签表里的一级指标名

                label = new Label();
                label.setIndexname(updateObject.getString("indexName"));
                label.setPackagelabel(packageName);

                List<Label> labelList = labelService.selectByThreeLabelName(label);

                List updateLabel = new ArrayList();
                if (labelList.size() > 0) {

                    for (int j = 0; j < labelList.size(); j++) {

                        label1 = new Label();
                        label1.setLabelid(labelList.get(j).getLabelid());
                        label1.setIndexname(updateObject.getString("newIndexName"));//新一级指标名

                        int b = labelService.updateLabel(label1);

                        if (b > 0) {
                            updateLabel.add(b);
                        } else {
                            map.put("code", 207);
                            map.put("message", "修改标签表失败");
//                            map.put("data", "");
                            return map;
                        }
                    }
                    if (labelList.size() == updateLabel.size()) {
                        //System.out.println("修改标签表成功");
                    } else {
                        map.put("code", 207);
                        map.put("message", "修改标签表失败");
//                        map.put("data", "");
                        return map;
                    }
                } else {
                    //System.out.println("查询无标签");
//                    map.put("code", 209);
//                    map.put("message", "查询无标签");
//                    map.put("data", updateObject.getString("indexName"));
                  //  return map;
                }
            }
                if (updateList.size()==list1.size()){
                    List<LabelIndex> labelIndexList=labelService.selectByPackageName(packageName);

                    map.put("code", 200);
                    map.put("message", "修改一级指标成功");
                    map.put("data", labelIndexList);

                }else {
                    map.put("code", 400);
                    map.put("message", "修改失败");
//                    map.put("data", "");
                    return map;
                }

        }else {
            map.put("code", 208);
            map.put("message", "该标签库没有值");
//            map.put("data", "");
            return map;
        }

        return map;
    }


    /**
     * 添加二级指标
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "insertTwoIndexLabel")
    public synchronized @ResponseBody Map<String,Object> insertTwoIndexLabel(HttpServletRequest request)throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();


        String allInformation=request.getParameter("allInformation").toString();//传入的所有信息
        //allInformation：{"teacherId":"5177","packageName":"活动标签","indexName":"所在一级指标","insert":[{"twoIndexName":"二级指标1","proportion":0.3},
        // {"twoIndexName":"二级指标2","proportion":0.25}],"update":[{"twoIndexName":"二级指标3","proportion":0.2},
        // {"twoIndexName":"二级指标4","proportion":0.25}]}

        //index为json类型，insert内包括要添加的指标及其权重，update内是之前标签库里的要修改权重的指标和修改后的权重，如果不修改，权重和原来一样
        //所有权重相加必须=1   要添加的指标名不能与原有指标名相同
        JSONObject json=JSONObject.fromObject(allInformation);
        String teacherId=json.getString("teacherId");//教师id  要添加日志
        String packageName=json.getString("packageName");//所属标签库
        String indexName=json.getString("indexName");//所属一级指标名

        //System.out.println(packageName+"*****"+indexName);

        String insert=json.getString("insert");//添加的指标信息 jsonArray
        String update=json.getString("update");//修改的指标信息 jsonArray

        JSONArray insertArray=JSONArray.fromObject(insert);
        JSONArray updateArray=JSONArray.fromObject(update);

        //System.out.println(updateArray.size()+"-----"+insertArray.size());

        LabelTwoIndex lti=new LabelTwoIndex();
        lti.setPackagelabel(packageName);
        lti.setIndexname(indexName);
        List<LabelTwoIndex> labelTwoIndexes=labelService.selectBylabelName(lti);

        //System.out.println("查出来的长度："+labelTwoIndexes.size());
        if (labelTwoIndexes.size()>0){

            //此标签库原本就有标签！！
            //System.out.println(labelIndexes.size()+"====="+updateArray.size());
            if (updateArray.size()!=labelTwoIndexes.size()){//长度不对
                map.put("code", 201);
                map.put("message", "修改的指标不匹配");
//                map.put("data", "");
                return map;
            }

            List<LabelTwoIndex> labelTwoIndexes1=new ArrayList<LabelTwoIndex>(labelTwoIndexes);//复制一个list
            List list=new ArrayList();//存所有权重的list
            //System.out.println("复制后的长得："+labelIndexes1.size());

            List indexn=new ArrayList();
            List indexn1=new ArrayList();

            for (int i=0;i<updateArray.size();i++){//比较是否相等
                JSONObject updateObject=updateArray.getJSONObject(i);
                indexn.add(updateObject.get("twoIndexName"));
                list.add(updateObject.get("proportion"));
            }

            for (int j=0;j<labelTwoIndexes1.size();j++){
                indexn1.add(labelTwoIndexes1.get(j).getTwoindexname());
            }
            // System.out.println(indexn.size()+"   "+indexn1.size());
            Collections.sort(indexn);
            Collections.sort(indexn1);
            for (int i=0;i<indexn.size();i++){
                if (!indexn.get(i).equals(indexn1.get(i))){
                    map.put("code", 201);
                    map.put("message", "修改的指标不匹配");
//                    map.put("data", indexn.get(i));
                    return map;
                }
            }
            for (int i=0;i<insertArray.size();i++) {
                JSONObject insertObject = insertArray.getJSONObject(i);
                for (int j=0;j<labelTwoIndexes.size();j++){
                    String twoIndexName = insertObject.getString("twoIndexName");//要添加的一级指标名
                    if (twoIndexName.equals(labelTwoIndexes.get(j).getTwoindexname())){
                        map.put("code", 202);
                        map.put("message", "指标"+twoIndexName+"已存在");
//                        map.put("data", labelTwoIndexes.get(j));
                        return map;
                    }
                }
                list.add(insertObject.get("proportion"));//添加加入的指标权重到list
            }
            //判断传入的权重相加为不为1
            Float sum=0f;
            for (Object o:list){
                sum+=Float.parseFloat(o.toString());
            }
            if (sum!=1){
                map.put("code", 203);
                map.put("message", "指标权重不为1，请重置");
//                map.put("data", "");
                return map;
            }

            LabelTwoIndex labelTwoIndex=null;
            List la=new ArrayList();
            List lb=new ArrayList();
            int a=0,b=0;
            for (int i=0;i<insertArray.size();i++) {//添加指标
                JSONObject insertObject = insertArray.getJSONObject(i);
                Float proportion=Float.parseFloat(insertObject.get("proportion").toString());
                labelTwoIndex=new LabelTwoIndex();
                labelTwoIndex.setIndexname(indexName);
                labelTwoIndex.setPackagelabel(packageName);
                labelTwoIndex.setTwoindexname(insertObject.getString("twoIndexName"));
                labelTwoIndex.setProportion(proportion);

                a=labelService.insertTwoIndex(labelTwoIndex);

                if (a>0){
                    //添加日志！！
                    //操作内容
                    String logContent="添加二级指标“"+insertObject.getString("twoIndexName")+"”成功";
                    //System.out.println(logContent);
                    logController.logManage(logContent, teacherId);

                    la.add(a);
                }

            }
            for (int i=0;i<updateArray.size();i++) {

                JSONObject updateObject=updateArray.getJSONObject(i);
                Float proportion=Float.parseFloat(updateObject.get("proportion").toString());
                labelTwoIndex=new LabelTwoIndex();
                labelTwoIndex.setPackagelabel(packageName);
                labelTwoIndex.setTwoindexname(updateObject.getString("twoIndexName"));
                labelTwoIndex.setProportion(proportion);
                b=labelService.updateByTwoIndexName(labelTwoIndex);
                if (b>0){
                    lb.add(b);
                }
            }
            LabelTwoIndex lti1=new LabelTwoIndex();
            lti1.setPackagelabel(packageName);
            lti1.setIndexname(indexName);
            List<LabelTwoIndex> labelTwoIndexes2=labelService.selectBylabelName(lti1);

            if (la.size()==insertArray.size()&&lb.size()==updateArray.size()) {

                map.put("code", 200);
                map.put("message", "添加二级指标成功");
                map.put("data", labelTwoIndexes2);

            }else {
                map.put("code", 400);
                map.put("message", "添加二级指标失败");
//                map.put("data", labelTwoIndexes2);
            }


        }else {
            //此标签库原本没有标签！！

            List list = new ArrayList();
            Float sum = 0f;
            for (int i = 0; i < insertArray.size(); i++) {
                JSONObject jsonObject = insertArray.getJSONObject(i);

                Float proportion = Float.parseFloat(jsonObject.getString("proportion"));//指标权重

                sum += proportion;

            }
           // System.out.println("和：" + sum);
            if (sum != 1) {
                map.put("code", 203);
                map.put("message", "权重设置不为1，请重置");
//                map.put("data", "");
            } else {

                LabelTwoIndex labelTwoIndex = null;
                for (int i = 0; i < insertArray.size(); i++) {
                    JSONObject jsonObject = insertArray.getJSONObject(i);

                    String twoIndexName = jsonObject.getString("twoIndexName");//二级指标名
                    Float proportion = Float.parseFloat(jsonObject.getString("proportion"));//指标权重

                    labelTwoIndex = new LabelTwoIndex();
                    labelTwoIndex.setIndexname(indexName);
                    labelTwoIndex.setPackagelabel(packageName);
                    labelTwoIndex.setTwoindexname(twoIndexName);
                    labelTwoIndex.setProportion(proportion);

                    int a = labelService.insertTwoIndex(labelTwoIndex);

                    if (a>0){
                        //添加日志！！
                        //操作内容
                        String logContent="添加二级指标“"+twoIndexName+"”成功";
                       // System.out.println(logContent);
                        logController.logManage(logContent, teacherId);
                        list.add(a);
                    }
                }

                LabelTwoIndex lti1=new LabelTwoIndex();
                lti1.setPackagelabel(packageName);
                lti1.setIndexname(indexName);
                List<LabelTwoIndex> labelTwoIndexes2=labelService.selectBylabelName(lti1);
                if (insertArray.size() == list.size()) {
                    //成功
                    map.put("code", 200);
                    map.put("message", "添加二级指标成功");
                    map.put("data", labelTwoIndexes2);
                } else {
                    //失败
                    map.put("code", 400);
                    map.put("message", "添加二级指标失败");
//                    map.put("data", "");
                }
            }
        }
        return map;

    }


    /**
     * 修改二级指标
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateTwoIndexLabel")
    public synchronized @ResponseBody Map<String,Object> updateTwoIndexLabel(HttpServletRequest request)throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String allInformation = request.getParameter("allInformation").toString();//传入的所有信息
        //allInformation：
        //{"teacherId":"5177","packageName":"活动标签","indexName":"一级指标1","update":[{"twoIndexName":"二级指标1","newTwoIndexName":"二级指标1.1",
        // "proportion":0.3},{"twoIndexName":"二级指标2","newTwoIndexName":"二级指标2","proportion":0.2},
        // {"twoIndexName":"二级指标3","newTwoIndexName":"二级指标3","proportion":0.3},
        // {"twoIndexName":"二级指标4","newTwoIndexName":"二级指标4.4","proportion":0.2}]}

        //update内有三个参数，twoIndexName，newTwoIndexName为修改后的指标名，如果不修改，则与twoIndexName相同，proportion为修改后的权重

        JSONObject json = JSONObject.fromObject(allInformation);
        String teacherId = json.getString("teacherId");//教师id  要添加日志
        String packageName = json.getString("packageName");//所属标签库
        String indexName=json.getString("indexName");//所属一级指标

        String update = json.getString("update");//修改的指标信息 jsonArray

        JSONArray updateArray = JSONArray.fromObject(update);

        LabelTwoIndex labelTwo=new LabelTwoIndex(indexName,packageName);

        List<LabelTwoIndex> labelTwoIndexes = labelService.selectBylabelName(labelTwo);
        if (labelTwoIndexes.size() > 0) {
            List indexn = new ArrayList();
            List indexn1 = new ArrayList();
            List list = new ArrayList();
            for (int i = 0; i < labelTwoIndexes.size(); i++) {
                indexn.add(labelTwoIndexes.get(i).getTwoindexname());
            }
            JSONObject updateObject = null;
            JSONObject updateObject1 = null;
            for (int i = 0; i < updateArray.size(); i++) {
                updateObject = updateArray.getJSONObject(i);
                if (labelTwoIndexes.size() != updateArray.size()) {
                    map.put("code", 201);
                    map.put("message", "指标不匹配");
//                    map.put("data", "");
                    return map;
                }
                for (int j = 0; j < updateArray.size() && j != i; j++) {
                    updateObject1 = updateArray.getJSONObject(j);
                    if (updateObject1.get("newTwoIndexName").equals(updateObject.get("newTwoIndexName"))) {
                        map.put("code", 202);
                        map.put("message", "指标名不能相同");
//                        map.put("data", "");
                        return map;
                    }
                }

                list.add(updateObject.get("proportion"));//权重
                indexn1.add(updateObject.get("twoIndexName"));
            }
            Collections.sort(indexn);
            Collections.sort(indexn1);
            for (int i = 0; i < indexn.size(); i++) {
                if (!indexn.get(i).equals(indexn1.get(i))) {
                    map.put("code", 201);
                    map.put("message", "指标不匹配");
//                    map.put("data", indexn1.get(i));
                    return map;
                }
            }
            Float sum = 0f;
            for (Object o : list) {
                sum += Float.parseFloat(o.toString());
            }
            if (sum != 1) {
                map.put("code", 203);
                map.put("message", "指标权重不为1，请重置");
//                map.put("data", "");
                return map;
            }


            //修改二级指标
            LabelIndex li=null,li1=null;
            List list1=new ArrayList();
            List updateList=new ArrayList();
            LabelTwoIndex labelTwoIndex=null,labelTwoIndex1=null;
            LabelThreeIndex labelThreeIndex=null,labelThreeIndex1=null;
            Label label=null,label1=null;
            for (int i=0;i<updateArray.size();i++) {
                updateObject = updateArray.getJSONObject(i);

                updateList.add(updateObject.get("twoIndexName"));

                labelTwoIndex = new LabelTwoIndex();
                labelTwoIndex.setTwoindexname(updateObject.getString("twoIndexName"));
                labelTwoIndex.setPackagelabel(packageName);

                LabelTwoIndex labelTwoIndex2 = evaluateTeacherService.selectByTwoIndexName(labelTwoIndex);
                if (labelTwoIndex2 != null) {

                    Float proportion = Float.parseFloat(updateObject.getString("proportion"));
                    labelTwoIndex1 = new LabelTwoIndex();
                    labelTwoIndex1.setTwoindexid(labelTwoIndex2.getTwoindexid());
                    labelTwoIndex1.setTwoindexname(updateObject.getString("newTwoIndexName"));
                    labelTwoIndex1.setProportion(proportion);

                    int a = labelService.updateByTwoIndexId(labelTwoIndex1);

                    if (a > 0) {

                        //添加日志
                        String logContent="修改二级指标“"+updateObject.getString("twoIndexName")+"”为“"+updateObject.getString("newTwoIndexName")+"”成功";
                        //System.out.println(logContent);
                        logController.logManage(logContent, teacherId);

                        list1.add(a);
                    }

                } else {
                    map.put("code", 204);
                    map.put("message", "指标查询出错");
//                    map.put("data", "");
                    return map;
                }

                //修改三级指标表里的二级指标名

                labelThreeIndex = new LabelThreeIndex();
                labelThreeIndex.setTwoindexname(updateObject.getString("twoIndexName"));
                labelThreeIndex.setPackagelabel(packageName);

                List<LabelThreeIndex> labelThreeIndexes = labelService.selectByTwoLabelName(labelThreeIndex);

                List updateThree = new ArrayList();
                if (labelThreeIndexes.size() > 0) {

                    for (int j = 0; j < labelThreeIndexes.size(); j++) {

                        labelThreeIndex1 = new LabelThreeIndex();
                        labelThreeIndex1.setThreeindexid(labelThreeIndexes.get(j).getThreeindexid());
                        labelThreeIndex1.setTwoindexname(updateObject.getString("newTwoIndexName"));//新二级指标名

                        int b = labelService.updateByThreeIndexId(labelThreeIndex1);

                        if (b > 0) {
                            updateThree.add(b);
                        } else {
                            map.put("code", 205);
                            map.put("message", "修改三级指标表失败");
//                            map.put("data", "");
                            return map;
                        }
                    }
                    if (labelThreeIndexes.size() == updateThree.size()) {
                        //System.out.println("修改三级指标表成功");
                    } else {
                        map.put("code", 205);
                        map.put("message", "修改三级指标表失败");
//                        map.put("data", "");
                        return map;
                    }
                }else {
                    //System.out.println("查询无三级指标");
//                    map.put("code", 205);
//                    map.put("message", "查询无三级指标");
//                    map.put("data", "");
//                    return map;
                }


                //修改标签表里的二级指标名

                label = new Label();
                label.setTwoindexname(updateObject.getString("twoIndexName"));
                label.setPackagelabel(packageName);

                List<Label> labelList = labelService.selectByThreeLabelName(label);

                List updateLabel = new ArrayList();
                if (labelList.size() > 0) {

                    for (int j = 0; j < labelList.size(); j++) {

                        label1 = new Label();
                        label1.setLabelid(labelList.get(j).getLabelid());
                        label1.setTwoindexname(updateObject.getString("newTwoIndexName"));//新二级指标名

                        int b = labelService.updateLabel(label1);

                        if (b > 0) {
                            updateLabel.add(b);
                        } else {
                            map.put("code", 206);
                            map.put("message", "修改标签表失败");
//                            map.put("data", "");
                            return map;
                        }
                    }
                    if (labelList.size() == updateLabel.size()) {
                        //System.out.println("修改标签表成功");
                    } else {
                        map.put("code", 206);
                        map.put("message", "修改标签表失败");
//                        map.put("data", "");
                        return map;
                    }
                } else {
                    //System.out.println("查询无标签");
//                    map.put("code", 207);
//                    map.put("message", "查询无标签");
//                    map.put("data", "");
//                    return map;
                }
            }
            if (updateList.size()==list1.size()){
                map.put("code", 200);
                map.put("message", "修改二级指标成功");
                map.put("data", updateList);

            }else {
                map.put("code", 400);
                map.put("message", "修改二级指标失败");
//                map.put("data", "");
                return map;
            }

        }else {
            map.put("code", 207);
            map.put("message", "该标签库没有值");
//            map.put("data", "");
            return map;
        }


        return map;
    }



    /**
     * 添加三级指标
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "insertThreeIndexLabel")
    public synchronized @ResponseBody Map<String,Object> insertThreeIndexLabel(HttpServletRequest request)throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();


        String allInformation=request.getParameter("allInformation").toString();//传入的所有信息
        //allInformation：{"teacherId":"5177","packageName":"活动标签","indexName":"所在一级指标","twoIndexName":"所在二级指标","insert":
        // [{"threeIndexName":"三级指标1","proportion":0.3},{"threeIndexName":"三级指标2","proportion":0.25}],
        // "update":[{"threeIndexName":"三级指标3","proportion":0.2},{"threeIndexName":"三级指标4","proportion":0.25}]}

        //index为json类型，insert内包括要添加的指标及其权重，update内是之前标签库里的要修改权重的指标和修改后的权重，如果不修改，权重和原来一样
        //所有权重相加必须=1   要添加的指标名不能与原有指标名相同
        JSONObject json=JSONObject.fromObject(allInformation);
        String teacherId=json.getString("teacherId");//教师id  要添加日志
        String packageName=json.getString("packageName");//所属标签库
        String indexName=json.getString("indexName");//所属一级指标名
        String twoIndexName=json.getString("twoIndexName");//所属二级指标

        //System.out.println(packageName+"*****"+indexName);

        String insert=json.getString("insert");//添加的指标信息 jsonArray
        String update=json.getString("update");//修改的指标信息 jsonArray

        JSONArray insertArray=JSONArray.fromObject(insert);
        JSONArray updateArray=JSONArray.fromObject(update);

        //System.out.println(updateArray.size()+"-----"+insertArray.size());

        LabelThreeIndex lti=new LabelThreeIndex();
        lti.setPackagelabel(packageName);
        lti.setTwoindexname(twoIndexName);
        List<LabelThreeIndex> labelThreeIndexes=labelService.selectByTwoLabelName(lti);

        //System.out.println("查出来的长度："+labelTwoIndexes.size());
        if (labelThreeIndexes.size()>0){

            //此标签库原本有标签！！
            //System.out.println(labelIndexes.size()+"====="+updateArray.size());
            if (updateArray.size()!=labelThreeIndexes.size()){//长度不对
                map.put("code", 201);
                map.put("message", "修改指标不匹配");
//                map.put("data", "");
                return map;
            }

            List<LabelThreeIndex> labelThreeIndexes1=new ArrayList<LabelThreeIndex>(labelThreeIndexes);//复制一个list
            List list=new ArrayList();//存所有权重的list
            //System.out.println("复制后的长得："+labelIndexes1.size());

            List indexn=new ArrayList();
            List indexn1=new ArrayList();

            for (int i=0;i<updateArray.size();i++){//比较是否相等
                JSONObject updateObject=updateArray.getJSONObject(i);
                indexn.add(updateObject.get("threeIndexName"));
                list.add(updateObject.get("proportion"));
            }

            for (int j=0;j<labelThreeIndexes1.size();j++){
                indexn1.add(labelThreeIndexes1.get(j).getTwoindexname());
            }
            // System.out.println(indexn.size()+"   "+indexn1.size());
            Collections.sort(indexn);
            Collections.sort(indexn1);
            for (int i=0;i<indexn.size();i++){
                if (!indexn.get(i).equals(indexn1.get(i))){
                    map.put("code", 201);
                    map.put("message", "修改指标不匹配");
//                    map.put("data", indexn.get(i));
                    return map;
                }
            }
            for (int i=0;i<insertArray.size();i++) {
                JSONObject insertObject = insertArray.getJSONObject(i);
                for (int j=0;j<labelThreeIndexes.size();j++){
                    String threeIndexName = insertObject.getString("threeIndexName");//要添加的三级指标名
                    if (threeIndexName.equals(labelThreeIndexes.get(j).getThreeindexname())){
                        map.put("code", 202);
                        map.put("message", "指标"+threeIndexName+"已存在");
//                        map.put("data", labelThreeIndexes.get(j));
                        return map;
                    }
                }
                list.add(insertObject.get("proportion"));//添加加入的指标权重到list
            }
            //判断传入的权重相加为不为1
            Float sum=0f;
            for (Object o:list){
                sum+=Float.parseFloat(o.toString());
            }
            if (sum!=1){
                map.put("code", 203);
                map.put("message", "指标权重不为1，请重置");
//                map.put("data", "");
                return map;
            }

            LabelThreeIndex labelThreeIndex=null;
            List la=new ArrayList();
            List lb=new ArrayList();
            int a=0,b=0;
            for (int i=0;i<insertArray.size();i++) {//添加指标
                JSONObject insertObject = insertArray.getJSONObject(i);
                Float proportion=Float.parseFloat(insertObject.get("proportion").toString());
                labelThreeIndex=new LabelThreeIndex();
                labelThreeIndex.setIndexname(indexName);
                labelThreeIndex.setPackagelabel(packageName);
                labelThreeIndex.setTwoindexname(twoIndexName);
                labelThreeIndex.setThreeindexname(insertObject.getString("threeIndexName"));
                labelThreeIndex.setProportion(proportion);

                a=labelService.insertThreeIndex(labelThreeIndex);

                if (a>0){

                    //添加日志！！
                    //操作内容
                    String logContent="添加三级指标“"+insertObject.getString("threeIndexName")+"”成功";
                    //System.out.println(logContent);
                    logController.logManage(logContent, teacherId);

                    la.add(a);
                }

            }
            for (int i=0;i<updateArray.size();i++) {

                JSONObject updateObject=updateArray.getJSONObject(i);
                Float proportion=Float.parseFloat(updateObject.get("proportion").toString());
                labelThreeIndex=new LabelThreeIndex();
                labelThreeIndex.setPackagelabel(packageName);
                labelThreeIndex.setThreeindexname(updateObject.getString("threeIndexName"));
                labelThreeIndex.setProportion(proportion);
                b=labelService.updateByThreeIndexName(labelThreeIndex);
                if (b>0){
                    lb.add(b);
                }
            }
            LabelThreeIndex lti1=new LabelThreeIndex();
            lti1.setPackagelabel(packageName);
            lti1.setTwoindexname(twoIndexName);
            List<LabelThreeIndex> labelThreeIndexes2=labelService.selectByTwoLabelName(lti1);

            if (la.size()==insertArray.size()&&lb.size()==updateArray.size()) {

                map.put("code", 200);
                map.put("message", "添加三级指标成功");
                map.put("data", labelThreeIndexes2);

            }else {
                map.put("code", 400);
                map.put("message", "添加三级指标失败");
//                map.put("data", labelThreeIndexes2);
            }


        }else {
            //此标签库原本没有标签！！

            List list = new ArrayList();
            Float sum = 0f;
            for (int i = 0; i < insertArray.size(); i++) {
                JSONObject jsonObject = insertArray.getJSONObject(i);

                Float proportion = Float.parseFloat(jsonObject.getString("proportion"));//指标权重

                sum += proportion;

            }
            //System.out.println("和：" + sum);
            if (sum != 1) {
                map.put("code", 203);
                map.put("message", "权重设置不为1，请重置");
//                map.put("data", "");
            } else {

                LabelThreeIndex labelThreeIndex = null;
                for (int i = 0; i < insertArray.size(); i++) {
                    JSONObject jsonObject = insertArray.getJSONObject(i);

                    String threeIndexName = jsonObject.getString("threeIndexName");//三级指标名
                    Float proportion = Float.parseFloat(jsonObject.getString("proportion"));//指标权重

                    labelThreeIndex = new LabelThreeIndex();
                    labelThreeIndex.setIndexname(indexName);
                    labelThreeIndex.setPackagelabel(packageName);
                    labelThreeIndex.setTwoindexname(twoIndexName);
                    labelThreeIndex.setThreeindexname(threeIndexName);
                    labelThreeIndex.setProportion(proportion);

                    int a = labelService.insertThreeIndex(labelThreeIndex);

                    if (a>0){
                        //添加日志！！
                        //操作内容
                        String logContent="添加三级指标“"+threeIndexName+"”成功";
                        //System.out.println(logContent);
                        logController.logManage(logContent, teacherId);

                        list.add(a);
                    }
                }
                LabelThreeIndex lti1=new LabelThreeIndex();
                lti1.setPackagelabel(packageName);
                lti1.setTwoindexname(twoIndexName);
                List<LabelThreeIndex> labelThreeIndexes2=labelService.selectByTwoLabelName(lti1);
                if (insertArray.size() == list.size()) {
                    //成功
                    map.put("code", 200);
                    map.put("message", "添加三级指标成功");
                    map.put("data", labelThreeIndexes2);
                } else {
                    //失败
                    map.put("code", 400);
                    map.put("message", "添加三级指标失败");
//                    map.put("data", labelThreeIndexes2);
                }
            }
        }
        return map;

    }


    /**
     * 修改三级指标
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateThreeIndexLabel")
    public synchronized @ResponseBody Map<String,Object> updateThreeIndexLabel(HttpServletRequest request)throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();

        String allInformation = request.getParameter("allInformation").toString();//传入的所有信息
        //allInformation：
        //{"teacherId":"5177","packageName":"活动标签","indexName":"一级指标1","twoIndexName":"三级指标1",
        // "update":[{"threeIndexName":"三级指标1","newThreeIndexName":"二级指标1.1","proportion":0.3},
        // {"threeIndexName":"三级指标2","newThreeIndexName":"三级指标2","proportion":0.2},
        // {"threeIndexName":"三级指标3","newThreeIndexName":"三级指标3","proportion":0.3},
        // {"threeIndexName":"三级指标4","newThreeIndexName":"三级指标4.4","proportion":0.2}]}

        //update内有三个参数，threeIndexName为原来的指标名，newThreeIndexName为修改后的指标名，如果不修改，则与threeIndexName相同，proportion为修改后的权重

        JSONObject json = JSONObject.fromObject(allInformation);
        String teacherId = json.getString("teacherId");//教师id  要添加日志
        String packageName = json.getString("packageName");//所属标签库
        String indexName=json.getString("indexName");//所属一级指标
        String twoIndexName=json.getString("twoIndexName");//所属二级指标

        String update = json.getString("update");//修改的指标信息 jsonArray

        JSONArray updateArray = JSONArray.fromObject(update);

        LabelThreeIndex labelThree=new LabelThreeIndex();
        labelThree.setTwoindexname(twoIndexName);
        labelThree.setPackagelabel(packageName);

        List<LabelThreeIndex> labelThreeIndexes = labelService.selectByTwoLabelName(labelThree);
        if (labelThreeIndexes.size() > 0) {
            List indexn = new ArrayList();
            List indexn1 = new ArrayList();
            List list = new ArrayList();
            for (int i = 0; i < labelThreeIndexes.size(); i++) {
                indexn.add(labelThreeIndexes.get(i).getThreeindexname());
            }
            JSONObject updateObject = null;
            JSONObject updateObject1 = null;
            for (int i = 0; i < updateArray.size(); i++) {
                updateObject = updateArray.getJSONObject(i);
                if (labelThreeIndexes.size() != updateArray.size()) {
                    map.put("code", 201);
                    map.put("message", "指标不匹配");
//                    map.put("data", "");
                    return map;
                }
                for (int j = 0; j < updateArray.size() && j != i; j++) {
                    updateObject1 = updateArray.getJSONObject(j);
                    if (updateObject1.get("newThreeIndexName").equals(updateObject.get("newThreeIndexName"))) {
                        map.put("code", 202);
                        map.put("message", "指标名不能相同");
//                        map.put("data", "");
                        return map;
                    }
                }

                list.add(updateObject.get("proportion"));//权重
                indexn1.add(updateObject.get("threeIndexName"));
            }
            Collections.sort(indexn);
            Collections.sort(indexn1);
            for (int i = 0; i < indexn.size(); i++) {
                if (!indexn.get(i).equals(indexn1.get(i))) {
                    map.put("code", 201);
                    map.put("message", "指标不匹配");
//                    map.put("data", indexn1.get(i));
                    return map;
                }
            }
            Float sum = 0f;
            for (Object o : list) {
                sum += Float.parseFloat(o.toString());
            }
            if (sum != 1) {
                map.put("code", 203);
                map.put("message", "指标权重不为1，请重置");
//                map.put("data", "");
                return map;
            }


            //修改三级指标
            LabelIndex li=null,li1=null;
            List list1=new ArrayList();
            List updateList=new ArrayList();
            LabelTwoIndex labelTwoIndex=null,labelTwoIndex1=null;
            LabelThreeIndex labelThreeIndex=null,labelThreeIndex1=null;
            Label label=null,label1=null;
            for (int i=0;i<updateArray.size();i++) {
                updateObject = updateArray.getJSONObject(i);

                updateList.add(updateObject.get("threeIndexName"));

                labelThreeIndex = new LabelThreeIndex();
                labelThreeIndex.setThreeindexname(updateObject.getString("threeIndexName"));
                labelThreeIndex.setPackagelabel(packageName);

                LabelThreeIndex labelThreeIndex2 = evaluateTeacherService.selectByThreeIndexName(labelThreeIndex);
                if (labelThreeIndex2 != null) {

                    Float proportion = Float.parseFloat(updateObject.getString("proportion"));
                    labelThreeIndex1 = new LabelThreeIndex();
                    labelThreeIndex1.setThreeindexid(labelThreeIndex2.getThreeindexid());
                    labelThreeIndex1.setThreeindexname(updateObject.getString("newThreeIndexName"));
                    labelThreeIndex1.setProportion(proportion);

                    int a = labelService.updateByThreeIndexId(labelThreeIndex1);

                    if (a > 0) {
                        //添加日志
                        String logContent="修改三级指标“"+updateObject.getString("threeIndexName")+"”为“"+updateObject.getString("newThreeIndexName")+"”成功";
                        //System.out.println(logContent);
                        logController.logManage(logContent, teacherId);

                        list1.add(a);
                    }

                } else {
                    map.put("code", 204);
                    map.put("message", "指标查询错误");
//                    map.put("data", "");
                    return map;
                }

                //修改标签表里的三级指标名

                label = new Label();
                label.setThreeindexname(updateObject.getString("threeIndexName"));
                label.setPackagelabel(packageName);

                List<Label> labelList = labelService.selectByThreeLabelName(label);

                List updateLabel = new ArrayList();
                if (labelList.size() > 0) {

                    for (int j = 0; j < labelList.size(); j++) {

                        label1 = new Label();
                        label1.setLabelid(labelList.get(j).getLabelid());
                        label1.setThreeindexname(updateObject.getString("newThreeIndexName"));//新三级指标名

                        int b = labelService.updateLabel(label1);

                        if (b > 0) {
                            updateLabel.add(b);
                        } else {
                            map.put("code", 205);
                            map.put("message", "修改标签表失败");
//                            map.put("data", "");
                            return map;
                        }
                    }
                    if (labelList.size() == updateLabel.size()) {
                        //System.out.println("修改标签表成功");
                    } else {
                        map.put("code", 205);
                        map.put("message", "修改标签表失败");
//                        map.put("data", "");
                        return map;
                    }
                } else {
                    //System.out.println("查询无标签");
//                    map.put("code", 207);
//                    map.put("message", "查询无标签");
//                    map.put("data", "");
//                    return map;
                }
            }
            LabelThreeIndex labelThreeIndex2=new LabelThreeIndex();
            labelThreeIndex2.setPackagelabel(packageName);
            labelThreeIndex2.setTwoindexname(twoIndexName);
            List<LabelThreeIndex> labelThreeIndexes1=labelService.selectByTwoLabelName(labelThreeIndex2);

            if (updateList.size()==list1.size()){
                map.put("code", 200);
                map.put("message", "修改三级指标成功");
                map.put("data", labelThreeIndexes1);

            }else {
                map.put("code", 400);
                map.put("message", "修改三级指标失败");
//                map.put("data", labelThreeIndexes1);
                return map;
            }


        }else {
            map.put("code", 206);
            map.put("message", "该标签库没有值");
//            map.put("data", "");
            return map;
        }

        return map;
    }

}
